#include "ConfigureWidget.h"

ConfigureWidget::ConfigureWidget(QWidget *parent)
    : MdiChild(parent)
{
    mq = new MQ();
    QVBoxLayout *pvlayout = new QVBoxLayout;
    ChildWidget(pvlayout);
    setLayout(pvlayout);
    m_conwidget->SetMq(mq);
    m_subwidget->SetMq(mq);
    setWindowTitle("ConfigureWidget");
}
void ConfigureWidget::ChildWidget(QVBoxLayout *playout)
{
    m_textedit0 = new QTextEdit;
    m_textedit1 = new QTextEdit;
    QHBoxLayout *phlayout = new QHBoxLayout;
    phlayout->addWidget(m_textedit0);
    phlayout->addWidget(m_textedit1);
    MdiChild::clientWidget(playout);
    m_conwidget = new ConnWidgte;
    m_subwidget = new SubWidget;
    playout->addWidget(m_conwidget);
    playout->addWidget(m_subwidget);
    playout->addLayout(phlayout);
}
ConfigureWidget::~ConfigureWidget()
{
    if(m_conwidget)
    {
        delete m_conwidget;
        m_conwidget = NULL;
    }

    if(m_subwidget)
    {
        delete m_subwidget;
        m_subwidget = NULL;
    }

    if(mq)
    {
        delete mq;
        mq = NULL;
    }
}
void ConfigureWidget::RecvData(char* buf, int len)
{
    MdiChild::RecvData(buf, len);
    QString str = "recv:";

    for(int ii = 0; ii < len; ii++)
    {
        str.append(QString("%1 ").arg(buf[ii] & 0xFF, 2, 16, QChar('0')));
    }

    m_textedit0->append(str);

    if(mq == NULL)
    {
        return;
    }

    mq->Recv(buf, len);
}
void ConfigureWidget::SendData()
{
    if(mq == NULL)
    {
        return;
    }

    char buff[2048];
    memset(buff, 0, 2048);
    int len0 = mq->Send(buff);
    QString str = "send:";

    for(int ii = 0; ii < len0; ii++)
    {
        str.append(QString("%1 ").arg(buff[ii] & 0xFF, 2, 16, QChar('0')));
    }

    if(len0 > 0)
    {
        m_textedit1->append(str);
        MdiChild::SendData0(buff, len0);
    }
}